package com.buka.user.controller;


import com.buka.entity.Result;
import com.buka.server.pojo.Address;
import com.buka.user.service.AddressService;
import com.buka.utils.BaseExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 地址管理
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 新增地址
     *
     * @param address
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Address address) {
        try {
            addressService.add(address);
        } catch (Exception e) {
            new BaseExceptionHandler<>().error("添加失败");
        }
        return new Result<>("添加成功");
    }

    /**
     * 修改地址
     *
     * @param address
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Address address) {
        try {
            addressService.update(address);
        } catch (Exception e) {
            new BaseExceptionHandler<>().error("修改失败");
        }
        return new Result<>("修改成功");
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam Long id) {
        try {
            addressService.delete(id);
        } catch (Exception e) {
            new BaseExceptionHandler<>().error("删除失败");
        }
        return new Result<>("删除成功");
    }

    /**
     * 用户名查询地址列表
     *
     * @return
     */
    @GetMapping("/findList")
    public Result findList() {
        List<Address> list = null;
        try {
            list = addressService.findList();
        } catch (Exception e) {
            return new Result<>("查询地址失败");
        }
        return new Result<>("查询地址成功", list);
    }


}
